package Job;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * description:原子操作类的多个方法调用并不构成原子性
 * author:zcx
 * lenovo
 * 时间：2022.02.08.14.27
 */
public class Job9 {
    AtomicInteger count = new AtomicInteger(0);

   synchronized void m() {
        for (int i = 0; i < 10000; i++) {
            if (count.get() < 100 && count.get() >= 0) { //如果未加锁,之间还会有其他线程插进来
                count.incrementAndGet();
            }
        }
    }

    public static void main(String[] args) {
      var t=new Job9();
        for (int i = 0; i < 10; i++) {
            new Thread(t::m,"T"+i).start();
        }
        try {
            TimeUnit.SECONDS.sleep(1);
            System.out.println(t.count.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
